package org.ws.edu.task3.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.ws.edu.task3.model.Answer;
import org.ws.edu.task3.model.Question;
import org.ws.edu.task3.model.Test;
import org.ws.edu.task3.model.Type;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.XStreamException;

public class Reader {
    public static void main(String[] args) {
        XStream xs = new XStream();
        Test e = new Test();

        xs.alias("test", Test.class);
        xs.alias("question", Question.class);
        xs.alias("answer", Answer.class);

        xs.addImplicitCollection(Test.class, "questions");
        xs.addImplicitCollection(Question.class, "allAnswers");

        xs.useAttributeFor(Answer.class, "factor");
        xs.useAttributeFor(Answer.class, "name");
        xs.useAttributeFor(Question.class, "name");
        xs.useAttributeFor(Test.class, "name");

        File folder = new File("./tests");
        File[] files = folder.listFiles();
        for (File f : files) {
            try {
                FileInputStream in = new FileInputStream(f);
                xs.fromXML(in, e);
                if (e.getType() != Type.SCALES)
                    e.setResult(null);
                System.out.println(e.toString());
            } catch (FileNotFoundException e2) {

            } catch (XStreamException e3) {
                System.err.println("invalid xml");
            }
        }
    }
}
